package com.rs.controller;

import com.rs.entity.Class;
import com.rs.entity.Response;
import com.rs.entity.Student;
import com.rs.entity.UserVo;
import com.rs.entity.login.Role;
import com.rs.entity.login.User;
import com.rs.entity.vo.AddUserVO;
import com.rs.service.RoleService;
import com.rs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dehang
 * @date 2019/06/01
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index(Model model) {
        return "user/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Response list(Integer page, Integer limit,String name) {
        List<UserVo> users = userService.getUserList(page,limit,name);
        return Response.list(users,userService.getUserSum(name));
    }

    @RequestMapping("/addUser")
    public String addUser(Model model){
        //查询所有权限
        List<Role> roles=roleService.getAll();
        model.addAttribute("roles",roles);
        return "user/addUser";
    }


    @RequestMapping("/add")
    @ResponseBody
    public long addUser(@RequestBody AddUserVO addUserVO) {

        return userService.addUser(addUserVO);
    }

    @RequestMapping("/updateUser")
    public String updateUser(Model model ,int id){
        //查询所有权限
        List<Role> roles=roleService.getAll();
        model.addAttribute("roles",roles);
        UserVo userVo=userService.getUpdateUser(id);
        model.addAttribute("userVo",userVo);
        return "user/updateUser";
    }

    @RequestMapping("/update")
    @ResponseBody
    public long updateUser(@RequestBody AddUserVO addUserVO) {

        return userService.updateUser(addUserVO);
    }

    @RequestMapping("/del")
    @ResponseBody
    public void delUser(int id) {
        userService.delUser(id);
    }

    @RequestMapping("/reset")
    @ResponseBody
    public void reset(int id){
        String pwd="123456";
        userService.reset(id,pwd);
    }

    @RequestMapping("/changepwd")
    @ResponseBody
    public int changepwd(HttpServletRequest request, String pwd, String oldpwd) {
        User user=(User) request.getSession().getAttribute("user");
        return userService.changepwd(user.getId(),pwd,oldpwd);
    }


    @RequestMapping("/allocation")
    public String allocation(Model model, long id) {
        //已分配班级
        List<Class> havestudents=userService.getHaveStudent(id);
        //所有班级
        List<Class> students=userService.getStudent();
        for (Class s:students){
            for (Class hs:havestudents){
                if(s.getId()==hs.getId()){
                    s.setChecked(true);
                    break;
                }
            }
        }
        model.addAttribute("two", students);
        model.addAttribute("id",id);
        return "user/allocation";
    }

    @RequestMapping("/allot")
    @ResponseBody
    public String allot(String mid,int id){
        userService.allot(mid,id);
        return "success";
    }



}
